'''
Author: yongfa
Date: 2020-11-18 17:51:18
LastEditTime: 2020-11-19 23:53:17
LastEditors: yongfa
Description: 自定义异常
'''

from flask import json, request
from werkzeug.exceptions import HTTPException


class APIException(HTTPException):
    code = 200  # http 状态码
    message = '服务器未知错误'  # 异常信息
    error_code = 1  # 约定的异常码

    def __init__(self, error_code=None, message=None, headers=None):
        if error_code is not None:
            self.error_code = error_code
        if message is not None:
            self.message = message
        super(APIException, self).__init__()

    def get_body(self, environ=None, scope=None):
        body = dict(
            message=self.message,
            error_code=self.error_code,
            request_url=request.method + ' ' + self.get_url_no_param()
        )
        text = json.dumps(body)  # 返回文本
        return text

    def get_headers(self, environ=None, scope=None):
        return [('Content-type', 'application/json; charset=utf-8')]

    @staticmethod
    def get_url_no_param():
        full_path = str(request.full_path)
        main_path = full_path.split('?')[0]
        return main_path


############################################
# #########    基础类错误(0~9999)   #########
############################################
class Success(APIException):
    '''
    0: 操作成功
    1: 操作失败
    '''

    error_code = 0
    result = None
    message = 'ok'

    def __init__(self, result=None, error_code=None, message=None):
        if result is not None:
            if hasattr(result, 'to_dict'):
                self.result = result.to_dict()
            else:
                self.result = result

        super(Success, self).__init__(error_code, message)

    def get_body(self, environ=None, scope=None):
        body = dict(
            error_code=self.error_code,
            message=self.message,
            result=self.result
        )
        text = json.dumps(body)  # 返回文本
        return text


class ServerError(APIException):
    error_code = 1
    message = '服务器端异常'


class Failed(APIException):
    error_code = 1
    message = '失败'


class LimiterError(APIException):
    error_code = 1
    message = '你请求太快啦，稍后再试哦！'

############################################
# ######### 基础类错误(11000~12000) #########
############################################

# #########  权限相关(10000~10100)  ##########


class AuthFailed(APIException):
    error_code = 1
    message = '授权失败'


class Forbidden(APIException):
    error_code = 1
    message = '权限不足，禁止访问'


# #########  查询相关(10100~10200)  ##########
class NotFound(APIException):
    error_code = 1  # 约定的异常码
    message = '未查询到数据'  # 异常信息


class RepeatException(APIException):
    error_code = 1
    message = '重复数据'


class ParameterException(APIException):
    error_code = 1
    message = '参数错误'


# ######### Token相关(10200~10300) ##########
class TokenException(APIException):
    error_code = 1
    message = '无效的Token'


class UnauthorizedError(APIException):
    error_code = 1
    message = "未携带有效的令牌"


class TokenExpireException(APIException):
    error_code = 20001
    message = "Token已过期,请刷新Token"


class TokenRefreshException(APIException):
    error_code = 20002
    message = "refresh token已过期"


class TokenLogoutException(APIException):
    error_code = 20003
    message = "用户已退出登录,请重新登录"


# ######### 文件相关(10300~10400) ##########
class FileTooLargeException(APIException):
    error_code = 1
    message = '文件体积过大'


class FileTooManyException(APIException):

    error_code = 1
    message = '文件数量过多'


class FileExtensionException(APIException):
    error_code = 1
    message = '文件扩展名不符合规范'


class QiniuException(APIException):

    error_code = 1
    message = '七牛云配置参数异常'


# 数据相关

class DataformatError(APIException):
    error_code = 1
    message = '数据格式不对,请检查BODY结构'


class UserException(APIException):
    error_code = 1
    message = '用户名或密码错误'


class TaskException(APIException):
    error_code = 1
    message = '任务异常'


class EMailProtocolError(APIException):
    error_code = 1
    message = '邮件协议错误'


class EMailLoginError(APIException):
    error_code = 1
    message = '邮件登录失败'


class DownloadEMailError(APIException):
    error_code = 1
    message = '下载邮件失败'
